﻿Imports System.Data
Imports MyGeneration.dOOdads


Public Class rc_OrdenCompra

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property


#Region "OrdenCompra"

    Public Function get_Rango_Orden(conn As String) As String
        Try

            Dim rango As New Class_RC_Data.Class_RC.RANGODOCUMENTOS(conn)
            Dim numero As String = ""
            rango.Where.TipoDocumento.Value = "ORDENCOMPRA"
            rango.Where.Habilitado.Value = True

            If rango.Query.Load Then
                If rango.DefaultView.Table.Rows.Count > 0 Then
                    numero = rango.Prefijo.ToString() + rango.RangoActual.ToString().PadLeft(4, "0")
                    rango.RangoActual += 1
                    rango.Save()
                    Return numero
                Else
                    Return ""
                End If

            Else
                Return ""

            End If

        Catch ex As Exception
            MessageError = ex
            Return ""
        End Try

    End Function

    Public Function Insert_OrdenCompra(conn As String, fecha As DateTime, codProveedor As Integer, nroOrden As String,
                                       codResponsable As Integer, estado As String, codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim oc As New Class_RC_Data.Class_RC.ORDENCOMPRA(conn)

            oc.AddNew()
            oc.Fecha = fecha
            oc.CodProveedor = codProveedor

            If nroOrden = "" Then
                nroOrden = get_Rango_Orden(conn)
            End If

            oc.NroOrdenCompra = nroOrden

            oc.CodResponsable = codResponsable
            oc.EstadoOrdenCompra = estado
            oc.CodUsuario = codUsuario
            oc.FechaActualizacion = DateTime.Now
            oc.Save()

            tx.CommitTransaction()

            Return oc.CodOrdenCompra

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_OrdenCompra(conn As String, codOrden As Integer,
                                     fecha As DateTime, codProveedor As Integer, nroOrden As String,
                                       codResponsable As Integer, estado As String, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim oc As New Class_RC_Data.Class_RC.ORDENCOMPRA(conn)
            oc.Where.CodOrdenCompra.Value = codOrden

            If oc.Query.Load Then
                oc.Fecha = fecha
                oc.CodProveedor = codProveedor
                oc.NroOrdenCompra = nroOrden
                oc.CodResponsable = codResponsable
                oc.EstadoOrdenCompra = estado
                oc.CodUsuario = codUsuario
                oc.FechaActualizacion = DateTime.Now
                oc.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_OrdenCompra(conn As String, codOrden As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim oc As New Class_RC_Data.Class_RC.ORDENCOMPRA(conn)
            oc.Where.CodOrdenCompra.Value = codOrden

            If oc.Query.Load Then

                oc.MarkAsDeleted()
                oc.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_OrdenCompra_All(conn As String, codOrden As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, codProveedor As Integer, ruc As String,
                                   nroOrdenCompra As String, proveedor As String,
                                   codResponsable As Integer, estadoOrden As String) As DataTable
        Try

            Dim vOrden As New Class_RC_Data.Class_RC.VistaOrdenCompra(conn)

            If codOrden > 0 Then
                vOrden.Where.CodOrdenCompra.Value = codOrden
            End If

            If codProveedor > 0 Then
                vOrden.Where.CodProveedor.Value = codProveedor
            End If

            If codResponsable > 0 Then
                vOrden.Where.CodProveedor.Value = codProveedor
            End If

            If nroOrdenCompra <> "" Then
                vOrden.Where.NroOrdenCompra.Value = "%" & nroOrdenCompra & "%"
                vOrden.Where.NroOrdenCompra.Operator = WhereParameter.Operand.Like
            Else
                vOrden.Where.Fecha.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vOrden.Where.Fecha.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vOrden.Where.Fecha.Operator = WhereParameter.Operand.Between
            End If

            If estadoOrden <> "" Then
                vOrden.Where.EstadoOrdenCompra.Value = estadoOrden
            End If

            If proveedor <> "" Then
                vOrden.Where.Proveedor.Value = "%" + proveedor + "%"
                vOrden.Where.Proveedor.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vOrden.Where.Ruc.Value = "%" + ruc + "%"
                vOrden.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            vOrden.Query.AddOrderBy("fecha", WhereParameter.Dir.DESC)

            If vOrden.Query.Load Then
                If vOrden.DefaultView.Table.Rows.Count > 0 Then
                    Return vOrden.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_OrdenCompra_ByID(conn As String, codOrden As Integer) As DataTable
        Try

            Dim vOrden As New Class_RC_Data.Class_RC.VistaOrdenCompra(conn)

            If codOrden > 0 Then
                vOrden.Where.CodOrdenCompra.Value = codOrden
            End If

            If vOrden.Query.Load Then
                If vOrden.DefaultView.Table.Rows.Count > 0 Then
                    Return vOrden.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_OrdenCompra_ByNRO(conn As String, nroOrden As String, sinCompras As Boolean) As DataTable
        Try

            Dim vOrden As New Class_RC_Data.Class_RC.VistaOrdenCompra(conn)

            vOrden.Where.NroOrdenCompra.Value = nroOrden

            If sinCompras Then
                vOrden.Where.NroFactura.Value = 0
                vOrden.Where.NroFactura.Operator = WhereParameter.Operand.IsNull
            End If

            If vOrden.Query.Load Then
                If vOrden.DefaultView.Table.Rows.Count > 0 Then
                    Return vOrden.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_OrdenCompra_Impresion(conn As String, codOrden As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, codProveedor As Integer, ruc As String,
                                   nroOrdenCompra As String, proveedor As String,
                                   codResponsable As Integer, estadoOrden As String) As DataTable
        Try

            Dim vOrden As New Class_RC_Data.Class_RC.VistaOrdenCompraImpresion(conn)

            If codOrden > 0 Then
                vOrden.Where.CodOrdenCompra.Value = codOrden
            End If

            'If codProveedor > 0 Then
            '    vOrden.Where.CodProveedor.Value = codProveedor
            'End If

            'If codResponsable > 0 Then
            '    vOrden.Where.CodProveedor.Value = codProveedor
            'End If

            'If nroOrdenCompra <> "" Then
            '    vOrden.Where.NroOrdenCompra.Value = "%" & nroOrdenCompra & "%"
            '    vOrden.Where.NroOrdenCompra.Operator = WhereParameter.Operand.Like
            'Else
            '    vOrden.Where.Fecha.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
            '    vOrden.Where.Fecha.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
            '    vOrden.Where.Fecha.Operator = WhereParameter.Operand.Between
            'End If

            If estadoOrden <> "" Then
                vOrden.Where.EstadoOrdenCompra.Value = estadoOrden
            End If

            If proveedor <> "" Then
                vOrden.Where.Proveedor.Value = "%" + proveedor + "%"
                vOrden.Where.Proveedor.Operator = WhereParameter.Operand.Like
            End If

            If ruc <> "" Then
                vOrden.Where.Ruc.Value = "%" + ruc + "%"
                vOrden.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            'vOrden.Query.AddOrderBy("fecha", WhereParameter.Dir.DESC)

            If vOrden.Query.Load Then
                If vOrden.DefaultView.Table.Rows.Count > 0 Then
                    Return vOrden.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_OrdenCompra_Impresion_ByID(ByVal conn As String, ByVal codOrden As Integer) As DataTable
        Try

            Dim vOrden As New Class_RC_Data.Class_RC.VistaOrdenCompraImpresion(conn)

            If codOrden > 0 Then
                vOrden.Where.CodOrdenCompra.Value = codOrden
            End If




            If vOrden.Query.Load Then
                If vOrden.DefaultView.Table.Rows.Count > 0 Then
                    Return vOrden.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_OrdenCompra_Detalle(conn As String, codOrden As Integer, codMaterial As Integer, cantidad As Decimal) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEORDENCOMPRA(conn)

            detalle.AddNew()
            detalle.CodOrdenCompra = codOrden
            detalle.CodMaterial = codMaterial
            detalle.Cantidad = cantidad
            detalle.Save()

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_OrdenCompra_Detalle(conn As String, codOrden As Integer,
                                          codMaterial As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEORDENCOMPRA(conn)

            detalle.Where.CodOrdenCompra.Value = codOrden
            detalle.Where.CodMaterial.Value = codMaterial
            If detalle.Query.Load Then
                detalle.MarkAsDeleted()
                detalle.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_OrdenCompra_Detalles_All(conn As String, codOrden As Integer) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaOrdenCompraDetalle(conn)

            If codOrden > 0 Then
                vDetalle.Where.CodOrdenCompra.Value = codOrden
            End If

            'If descripcion <> "" Then
            '    vPro.Where.DescProducto.Value = "%" & descripcion & "%"
            '    vPro.Where.DescProducto.Operator = WhereParameter.Operand.Like
            'End If

            'If codTipoProducto > 0 Then
            '    vPro.Where.CodTipoProducto.Value = codTipoProducto
            'End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Update_OrdenCompra_Estado(ByVal conn As String, ByVal codOrdenCompra As Integer, ByVal estadoOrden As String, ByVal codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim ordencompra As New Class_RC_Data.Class_RC.ORDENCOMPRA(conn)
            ordencompra.Where.CodOrdenCompra.Value = codOrdenCompra

            If ordencompra.Query.Load Then
                ordencompra.EstadoOrdenCompra = estadoOrden
                'ordencompra.SetColumnNull("codOrdenCompra")
                ordencompra.CodUsuario = codUsuario
                ordencompra.FechaActualizacion = DateTime.Now
                ordencompra.Save()
            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function
#End Region

End Class
